
package JET.geom.curves;

import JET.graphics.GraphicPanel;
import java.awt.*;

public class Bezier extends ControlCurve {

  // the basis function for a Bezier spline
  static float b(int i, float t) {
    switch (i) {
    case 0:
      return (1-t)*(1-t)*(1-t);
    case 1:
      return 3*t*(1-t)*(1-t);
    case 2:
      return 3*t*t*(1-t);
    case 3:
      return t*t*t;
    }
    return 0; //we only get here if an invalid i is specified
  }

  //evaluate a point on the B spline
  Point p(int i, float t, GraphicPanel gp) {
    float px=0;
    float py=0;
    for (int j = 0; j<=3; j++){
      px += b(j,t)*pts.xpoints[i+j];
      py += b(j,t)*pts.ypoints[i+j];
    }
    return new Point((int)Math.round(px),(int)Math.round(py));
  }

    final int STEPS = 12;

    @Override
    public void paint(Graphics g, GraphicPanel gp) {
        //super.paint(g, gp);
        Polygon pol = new Polygon ();
        Point q = p(0, 0, gp);
        pol.addPoint((int)gp.engineToScrX(q.x),(int)gp.engineToScrY(q.y));
        for (int i = 0; i < pts.npoints-3; i+=3) {
            for (int j = 1; j <= STEPS; j++) {
                q = p(i,j/(float)STEPS, gp);
                pol.addPoint((int)gp.engineToScrX(q.x), (int)gp.engineToScrY(q.y));
            }
        }
        g.drawPolyline(pol.xpoints, pol.ypoints, pol.npoints);
    }

}